<template lang="html">
  <section class="components-common_register_panel-list">
    <h1>{{title}}: </h1>
    <p :class="{ showList: isShowList }" @click="isShowList = !isShowList">
      {{current.value}}
      <i class="ic-category-more"></i>
    </p>
    <ul class="options-list" v-show="isShowList">
      <li class="options-item"
        v-for="(item, index) in dist"
        @click="selectItem(index)"
      >
        {{item.value}}
      </li>
    </ul>
  </section>
</template>

<script>
export default {
  props: {
    name: {
      type: String,
    },
    title: {
      type: String,
    },
    defaultIndex: {
      type: Number,
      default: 0
    },
    dist: {
      type: Array,
      require: true
    }
  },
  data(){
    return {
      selectedIndex: '',
      isShowList: false
    }
  },
  computed: {
    current(){
      if(!this.dist || this.dist.length === 0) return ''
      return this.dist[this.selectedIndex]
    }
  },
  watch: {
    selectedIndex(val){
      this.$emit('dispatchValue', {
        key: this.name,
        value: val
      })
    }
  },
  created(){
    if(typeof this.defaultIndex === 'number'){
      this.selectedIndex = this.defaultIndex
    }
  },
  methods: {
    selectItem(index){
      this.selectedIndex = index
      this.isShowList = false
    }
  }
}
</script>

<style lang="scss" scoped>
@import "../../../css/config";
$paddingLeft: 15px;
.components-common_register_panel-list{
  position: relative;
  width: 100%;
  margin-bottom: 35px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  h1{
    font-size: 30px;
    color: #000;
    letter-spacing: 1Px;
  }
  p{
    position: relative;
    display: block;
    width: 80%;
    height: 50px;
    line-height: 50px;
    font-size: 28px;
    letter-spacing: 1Px;
    color: #000;
    padding-left: $paddingLeft;
    box-sizing: border-box;
    background-color: #e6e6e6;
    .ic-category-more{
      position: absolute;
      top: 0%;
      right: 15px;
      line-height: 50px;
      font-size: 12px;
      color: $common-red;
    }
    &.showList{
      color: rgba(0, 0, 0, 0.4);
      .ic-category-more{
        opacity: 0.4;
      }
    }
  }
  .options-list{
    position: fixed;
    z-index: 999;
    display: block;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 7rem;
    height: 10rem;
    background-color: #fff;
    border-radius: 10px;
    opacity: 0.9;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.4);
    overflow: scroll;
    .options-item{
      display: block;
      width: 100%;
      font-size: 28px;
      color: #000;
      padding: 12px 0 12px $paddingLeft;
      border-top: 1Px solid #fff;
      &:active{
        color: #fff;
        background-color: $common-red;
      }
    }
  }
}
</style>
